<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="600px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="80px" :disabled="disableEditSubmit">
      <el-row>
        <el-col :span="24">
          <el-form-item label="项目" prop="sourceid" :required="true" :rules="rules.validateRequire">
            <el-input v-model="model.dispsourceid" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmProjectRefDialog" v-if="!disableEditSubmit && isAdd && editFlag"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="权限" prop="extendfield1" :rules="rules.validateRequire">
            <el-select v-model="model.extendfield1" filterable clearable>
              <el-option v-for="item in codeModel.permissiontypeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="资料类型" prop="extendfield2" :rules="rules.validateRequire">
            <el-select v-model="model.extendfield2" filterable clearable>
              <el-option v-for="item in codeModel.datatypeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">
        <svg-icon icon-class="save" />保存
      </el-button>
      <tl-my-attachment ref="tlMyAttachment" sourceType="PmProjectAttachment" :id="model.id" :editable="disableEditSubmit === false" :limitCount="1" btnTitle="项目资料" v-if="!isAdd" @upload-success="onAttachmentUploadSuccess" @delete-success="onAttachmentDeleteSuccess"></tl-my-attachment>
    </div>
    <tlpm-project-ref ref="pmProjectRef" @confirm="getpmProjectRefData" :isSingleSelect="true"></tlpm-project-ref>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from "@tapui/mixins/mixin_edit"
/** 子组件 */
import tlpmProjectRef from "@/views/ProjectManage/pmProject/refList"
import tlMyAttachment from "@/components/myAttachment/myAttachment"
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from "@tapui/utils/http"
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: { tlpmProjectRef, tlMyAttachment },
  data() {
    return {
      moduleTitle: "项目资料",
      urlPrefix: "/pm/attachment/",
      url: {
        // 额外的url
        queryOrgByDeptId: "/pm/resourceArchives/queryOrgByDeptId"
      }
    }
  },
  props: {
    projectId: {
      type: [Number, String],
      default: function() {
        return ""
      }
    },
    editFlag: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    loaded() {
      // 页面加载完毕执行
      if (this.isAdd) {
        this.$set(this.model, "sourcetype", "PmProjectAttachment")
      } else {
        this.$refs.tlMyAttachment.queryFileCount()
      }
    },
    /** 打开项目参照 */
    openpmProjectRefDialog() {
      this.$refs.pmProjectRef.visible = true
    },
    /** 获取项目参数数据 */
    getpmProjectRefData(selectionRow, selectKey, selectionValue) {
      const { id, name } = selectionRow
      this.$set(this.model, "sourceid", id)
      this.$set(this.model, "dispsourceid", name)
    },
    onAttachmentUploadSuccess() {
      this.$emit("refresh")
    },
    onAttachmentDeleteSuccess() {
      this.$emit("refresh")
    },
    onSelectDept(selectedDepts) {
      const selectedDept = selectedDepts[0]
      const param = {
        deptId: selectedDept.id
      }
      getAction(this.url.queryOrgByDeptId, param).then((res) => {
        if (res.success) {
          const { code: orgCode, name: orgName } = res.result
          this.$set(this.model, "orgcode", orgCode)
          this.$set(this.model, "orgname", orgName)
        } else {
          this.$tap.alert("已选部门关联公司信息错误，请联系系统管理员")
        }
      })
    },
    /** 提交前执行 */
    beforeSubmit() {
      this.model = {
        id: this.model.id,
        sourceid: this.model.sourceid,
        sourcetype: this.model.sourcetype,
        extendfield1: this.model.extendfield1,
        extendfield2: this.model.extendfield2,
        sortcode: this.model.sortcode,
        enabled: this.model.enabled
      }
      return true
    },
    /** 执行新增提交 */
    doAdd() {
      this.saveLoading = true
      postAction(this.url.add, this.model)
        .then(res => {
          if (res.success) {
            this.$tap.message('添加成功', 'success')
            this.isAdd = false
            this.queryData(res.result.id).then(_ => {
              this.loaded()
              this.$refs.tlMyAttachment.openUploadDialog()
            })
            this.$emit('refresh')
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch(err => {
          this.saveLoading = false
        })
    }
  }
}
</script>
